package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.modular.system.entity.Site;
import cn.stylefeng.guns.sys.modular.system.mapper.SiteMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.util.List;
import java.util.Map;

/**
 * (Site)表服务实现类
 *
 * @author makejava
 * @since 2020-07-15 09:23:19
 */
@Service
public class SiteService extends ServiceImpl<SiteMapper, Site> {

    /**
     * 根据条件查询用户列表
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:45
     */
    public Page<Map<String, Object>> selectKmsList() throws Exception {
        Page page = LayuiPageFactory.defaultPage();
        Page<Map<String, Object>> mapPage = this.baseMapper.selectSiteList(page);
        List<Map<String, Object>> records = mapPage.getRecords();
        List<Map<String, Object>> newRecords = Lists.newArrayList();
        for (Map<String, Object> record : records){
            boolean flage = ping(record.get("ip").toString());
            if(flage) {
                record.put("status",1);
            }
            else {
                record.put("status",0);
            }
            newRecords.add(record);
        }
        mapPage.setRecords(newRecords);
        return mapPage;
    }
    public static boolean ping(String ipAddress) throws Exception {
        int  timeOut =  2000 ;  //超时应该在3钞以上
        boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);
        // 当返回值是true时，说明host是可用的，false则不可。
        return status;
    }
}